/**
 * 
 */
package optimization.naturalOptimization.terminationCriterion;

import optimization.naturalOptimization.NaturalOptimization;

/**
 * Termination Criterion to stop at a given fitness value
 * 
 * @author Kevin Wagner
 * @version 1.0
 */
public class FitnessTermination extends TerminationCriterion {

	private double stopFitness = Double.MAX_VALUE;

	/**
	 * @param opt
	 */
	public FitnessTermination(NaturalOptimization opt) {
		super(opt);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see optimization.IdentificationI#getIndentification()
	 */
	@Override
	public String getIndentification() {
		return "Fitness Termination Criterion\n  "
				+ "Stop Fitness:"
				+ getParentOptimization().getProperties().getProperty(
						"stopFitness", "0.0");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * optimization.naturalOptimization.terminationCriterion.TerminationCriterion
	 * #isFinished()
	 */
	@Override
	public boolean isFinished() {
		if (stopFitness == Double.MAX_VALUE) {
			stopFitness = Double.valueOf(getParentOptimization()
					.getProperties().getProperty("stopFitness", "0.0"));
		}
		double best = getParentOptimization().getResults().get(
				getParentOptimization().getCurrentGeneration())[0];
		int status = getParentOptimization().getFitness().compare(best,
				stopFitness);
		return status < 2;
	}

}
